<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../bangnuo/admin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../bangnuo/admin/style/admin.css" media="all">
</head>

<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-block">
                        <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-block">
                        <input type="text" name="roleName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">唯一标识</label>
                    <div class="layui-input-block">
                        <input type="text" name="roleCode" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" onclick="addRole()">新增角色</button>
            </div>

            <table id="role-demo" lay-filter="role-demo"></table>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={ { d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="give"><i
                        class="layui-icon layui-icon-set"></i>权限</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="../../bangnuo/admin/layui/layui.js"></script>
<script src="../../bangnuo/admin/js/jquery-1.8.0.min.js"></script>
</body>
<script type="text/javascript">
    function addRole() {
        layer.open({
            type: 2,
            title: "新增角色",
            area: ['500px', '340px'],
            content: 'operation/add-role.html' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });
    }
</script>
<script type="text/javascript">
    layui.use(['table', 'form'], function () {
        var table = layui.table,
            form = layui.form;
        //监听提交
        form.on('submit(LAY-user-front-search)', function (data) {
            var order = {
                "roleName": data.field.roleName,
                "roleCode":data.field.roleCode,
                "id": data.field.id
            };
            table.reload('role-demo', {
                url: '/permissionsEntity/roleAll',
                where: order,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //第一个实例
        table.render({
            elem: '#role-demo',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            url: '/permissionsEntity/roleAll',
            contentType: "application/json;charset=UTF-8",
            parseData: function (res) { //res 即为原始返回的数据
                console.log("res执行", res);
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.queryResult.total, //解析数据长度
                    "data": res.queryResult.list//解析数据列表
                };
            },
            page: true,
            method: "post",
            limit: 5,
            limits: [5, 10, 20],
            cols: [
                [ //表头
                    {
                        field: 'id',
                        title: 'ID',
                        sort: true,
                        fixed: 'left'
                    }, {
                    field: 'roleName',
                    title: '角色名'
                },
                    {
                        field: 'roleCode',
                        title: '唯一标识'
                    },{
                    field: '',
                    title: '操作',
                    width: 240,
                    templet: '#table-useradmin-webuser'
                }
                ]
            ]
        });

        //监听工具条
        table.on('tool(role-demo)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'give') { //查看
                //do somehing
                layer.open({
                    type: 2,
                    title: "赋予权限",
                    area: ['500px', '400px'],
                    content: 'operation/role-permissions.html?id=' + data.id //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                });
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type: "get",
                        url: "/permissionsEntity/delRole/" + data.id,
                        async: true,
                        success: function (data) {
                            if (data.success == true) {
                                layer.msg("删除成功");
                                window.location.reload();
                            } else {
                                layer.msg("删除失败");
                            }
                        }
                    });
                });
            } else if (layEvent === 'edit') { //编辑
                //do something
                layer.open({
                    type: 2,
                    title: "修改角色",
                    area: ['500px', '340px'],
                    content: 'operation/edit-role.html?id=' + data.id //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                });
                //同步更新缓存对应的值
                obj.update({
                    username: '123',
                    title: 'xxx'
                });
            } else if (layEvent === 'LAYTABLE_TIPS') {
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });

    });
</script>

</html>